// pages/record/main/main.js
const app = getApp();
const model = require('../../../utils/model');
const util = require('../../../utils/util');


Page({

  /**
   * 页面的初始数据
   */
  data: {
    path:'',
    status:'init',
    statusText: '',
    content:'',
    title:'',
    id:0
  },

  record: function () {
    var status = this.data.status, statusText;
    var me = this;
    if (status == 'init') {
      status = 'recording';
      statusText = '正在录制中...';

      wx.startRecord({
        success: function (res) {
          var tempFilePath = res.tempFilePath
          me.setData({
            path: tempFilePath
          });
          
        }
      })
    }
    else if (status == 'recording') {
      status = 'stop';
      statusText = '录制已结束...';

      wx.stopRecord();
    }
    else if (status == 'stop') {
      
    }
    this.setData({
      status:status,
      statusText:statusText
    });
  },

  reset:function(){
    this.setData({
      status:'init'
    });
  },

  tapsave:function(){
    this.save(function(res){
      if (res.data.code) {
        wx.showToast({
            title: res.data.msg,
            icon: 'success',
            duration: 2000,
            success:function(){
              wx.reLaunch({
                url:'/pages/record/choose_article/index'
              });
            }
        });
      }
      
    });
  },

  save: function (callback) {
    var filePath = this.data.path;
    var aid = this.data.id;
    var me = this;
    wx.uploadFile({
      url:'https://v1.wx91go.com/index.php?m=Api&c=WxMini&a=uploadMP3',
      filePath:filePath,
      header:{
        "Content-Type":"multipart/form-data"
      },
      name:'file',
      success:function(res){
        var path = JSON.parse(res.data).data;
        model.request({
          action:'saveReading',
          method:'POST',
          data:{
            id:aid,
            mp3_url:path
          },
          header:{
            "Content-Type":"application/x-www-form-urlencoded"
          },
          success:function (res) {
            var wid = res.data.id;
            if (callback) {
              callback(res,me.data.title);
            }
          }
        });
      }
    });

    // var me = this;
    // model.request({
    //   action: 'saveReading',
    //   method: 'POST',
    //   header: {
    //     "Content-Type": "application/x-www-form-urlencoded"
    //   },
    //   data: {
    //     id:this.data.id,
    //     mp3_url:''
    //   },
    //   success: function (res) {
    //     if (callback) {
    //       callback(res.data.id,me.data.title);
    //     }
    //     else {
    //       wx.showToast({
    //         title: res.data.msg,
    //         icon: 'success',
    //         duration: 2000
    //       });
    //     }
    //   }
    // });
  },

  tappublish:function(){
    this.save(function (res,title) {
      wx.navigateTo({
        url:'/pages/works/publish/publish?id=' + res.data.id + "&title=" + title+ "&refer=record_main"
      });
    });
  },

  getArticleDetail:function(id){
    var me = this;
    model.request({
      action:'info',
      data:{
        id:id
      },
      success: function (res) {
        //console.log(res.data.flash_list);
        me.setData({
          content:util.formatText(res.data)
        });
      }
    });

    model.request({
      action:'mwpdDetail',
      data:{
        id:id
      },
      success: function (res) {
        //console.log(res.data.flash_list);
        me.setData({
          title:res.data.detail.title
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getArticleDetail(options.id);
    this.setData({
      id:options.id,
      scrollHeight: app.globalData.winHeight - 93 - 122
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})